package com.mall507.fivepeaks.comn.adapter;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class LoopPagerAdapter extends PagerAdapter {
    private List<View> views;

    public LoopPagerAdapter(List<View> views) {
        this.views = views;
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public Object instantiateItem(ViewGroup view, int index) {
        try {
            if (index >= views.size()) {
                view.removeView(views.get(views.size() - 1));
                view.addView(views.get(views.size() - 1));
                return views.get(views.size() - 1);
            } else {
                view.removeView(views.get(index));
                view.addView(views.get(index));
                return views.get(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return views.get(views.size() - 1);
        }
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        // ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {

    }

    @Override
    public void finishUpdate(View arg0) {

    }

}
